import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class ChatServer {
    static List<Socket> socketList=new ArrayList<Socket>();
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(9900);
            while (true){
                try {
                    Socket socket = serverSocket.accept();//服务器等待客户端连接
                    socketList.add(socket);
                    //输入流接收客户端传来的用户名  //oos.writeObject(userid);将用户名发送到服务器
                    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                    //4、服务器端获取登陆的用户名
                    String name = (String) ois.readObject();
                    //5、在服务器端打印登录的用户名
                    System.out.println(name+"加入聊天");
                    //6、服务器端获取对象输出流
                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    //7、服务器端向客户端响应 “name+",您好！欢迎您进入聊天室"”
                    oos.writeObject(name+",您好！欢迎您进入聊天室");
                    //11、引入多线程接受这个socket对应的客户端的聊天信息，支持可以处理多个客户端
                    new Thread(new ChatChannel(ois,oos,name)).start();
                    //new Thread(new ChatChannel(socketList,name)).start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*static class MyObjectOutputStream extends ObjectOutputStream {

        public MyObjectOutputStream(OutputStream out) throws IOException {
            super(out);
        }

        @Override
        protected void writeStreamHeader() throws IOException {
            //重写读取头部信息方法：不写入头部信息
            super.reset();
        }
    }

    static class MyObjectInputStream extends ObjectInputStream {
        public MyObjectInputStream(InputStream in) throws IOException {
            super(in);
        }

        @Override
        protected void readStreamHeader() throws IOException {
            //重写读取头部信息方法：什么也不做
        }
    }*/


}
